package pack;

/*
 * hashmap int int
 * put
 * remove
 * get
 * contains
 */
public class IntHashMap {
	
    private transient IntEntry[] table;
    //private transient int modCount;
    private transient int size;
    private static final int MAXIMUM_CAPACITY = 1 << 30;
    private static final float DEFAULT_LOAD_FACTOR = 0.75f;
    private static final int DEFAULT_INITIAL_CAPACITY = 16;
    private final float loadFactor;
    private int threshold;
    
    public IntHashMap(int initialCapacity, float loadFactor) {
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal initial capacity: " +
                                               initialCapacity);
        if (initialCapacity > MAXIMUM_CAPACITY)
            initialCapacity = MAXIMUM_CAPACITY;
        if (loadFactor <= 0 || Float.isNaN(loadFactor))
            throw new IllegalArgumentException("Illegal load factor: " +
                                               loadFactor);

        int capacity = 1;
        while (capacity < initialCapacity)
            capacity <<= 1;

        this.loadFactor = loadFactor;
        threshold = (int)Math.min(capacity * loadFactor, MAXIMUM_CAPACITY + 1);
        table = new IntEntry[capacity];
    }
    
    public int size() {
        return size;
    }
    
    public boolean isEmpty() {
        return size == 0;
    }
    
    public IntHashMap(int initialCapacity) {
        this(initialCapacity, DEFAULT_LOAD_FACTOR);
    }

    public IntHashMap() {
        this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR);
    }
    
    private void resize(int newCapacity) {
        IntEntry[] oldTable = table;
        int oldCapacity = oldTable.length;
        if (oldCapacity == MAXIMUM_CAPACITY) {
            threshold = Integer.MAX_VALUE;
            return;
        }

        IntEntry[] newTable = new IntEntry[newCapacity];
        transfer(newTable);
        table = newTable;
        threshold = (int)Math.min(newCapacity * loadFactor, MAXIMUM_CAPACITY + 1);
    }
    
    private void transfer(IntEntry[] newTable) {
        int newCapacity = newTable.length;
        for (IntEntry e : table) {
            while(null != e) {
                IntEntry next = e.next;
                e.hash = hash(e.getKey());
                int i = indexFor(e.hash, newCapacity);
                e.next = newTable[i];
                newTable[i] = e;
                e = next;
            }
        }
    }
    
    private void createEntry(int hash, int key, int value, int bucketIndex) {
        IntEntry e = table[bucketIndex];
        table[bucketIndex] = new IntEntry(hash, key, value, e);
        size++;
    }
    
    private void addEntry(int key, int value, int bucketIndex) {
        if ((size >= threshold) && (null != table[bucketIndex])) {
            resize(2 * table.length);
            bucketIndex = indexFor(hash(key), table.length);
            
        }
        createEntry(hash(key), key, value, bucketIndex);
    }
    
    private static int indexFor(int h, int length) {
        return h & (length-1);
    }
    
    public boolean containsKey(int key) {
        return getEntry(key) != null;
    }
    
    private final IntEntry getEntry(int key) {
        int hash = hash(key);
        for (IntEntry e = table[indexFor(hash, table.length)];
             e != null;
             e = e.next) {
            if (e.hash == hash && e.getKey() == key)
                return e;
        }
        return null;
    }
    
    public void put(int key, int value) {
        int hash = hash(key);
        int i = indexFor(hash, table.length);
        for (IntEntry e = table[i]; e != null; e = e.next) {
            if (e.hash == hash && (e.getKey() == key)) {
                e.setValue(value);
                return;
            }
        }
        //modCount++;
        addEntry(key, value, i);
    }
    
    public int remove(int key) {
        IntEntry e = removeEntryForKey(key);
        return (e == null ? null : e.getValue());
    }
    
    private final IntEntry removeEntryForKey(int key) {
        int hash = hash(key);
        int i = indexFor(hash, table.length);
        IntEntry prev = table[i];
        IntEntry e = prev;

        while (e != null) {
            IntEntry next = e.next;
            if (e.hash == hash && (e.getKey() == key)) {
                //modCount++;
                size--;
                if (prev == e)
                    table[i] = next;
                else
                    prev.next = next;
                return e;
            }
            prev = e;
            e = next;
        }

        return e;
    }
    
    private final int hash(int k) {
        int h = 0;
        h ^= k;
        h ^= (h >>> 20) ^ (h >>> 12);
        return h ^ (h >>> 7) ^ (h >>> 4);
    }
    
    public int get(int key) {
        IntEntry entry = getEntry(key);
        Integer value = entry.getValue();        	
        if(value != null) {
        	return value;
        } else {
        	return Integer.MIN_VALUE;
        }
    }


}
